package com.itheima.train.manager.search.configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Bean
    public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter(){

        // 创建一个json转换器
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter() ;

        // 获取转换json的工具类
        ObjectMapper objectMapper = mappingJackson2HttpMessageConverter.getObjectMapper();

        // 创建一个普通的模型类对象，用来定义类型的转换规则
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class , ToStringSerializer.instance) ;
        simpleModule.addSerializer(Long.TYPE , ToStringSerializer.instance) ;
        objectMapper.registerModule(simpleModule) ;

        // 返回
        return mappingJackson2HttpMessageConverter ;
    }

}
